/*
 * menu.c
 *
 *  Created on: 25/Abr/2010
 *      Author: ngela Igreja
 */

#include "menu.h"
#include "mouse.h"

#define SC_UP 0x48
#define SC_DOWN 0x50

void initMenu( char options[][32]  , Menu * menu , int num_options )
{
  int i = 0;
	menu->list = (Option *) malloc(4 * sizeof(Option));
	menu->title = (char *)malloc(32 * sizeof(char));
	menu->num_options = num_options;
	strcpy( menu->title , options[i] );
	for ( i = 1 ; i <= num_options ; ++i )
	{
		menu->list[i-1].name = (char * ) malloc (strlen(options[i]) * sizeof (char));
		strcpy( menu->list[i-1].name , options[i] );
		menu->list[i-1].select = ( (i-1) == 0 ? 1 : 0);
		menu->list[i -1].y_i = 240 + ( i - 1 ) * 60;
		menu->list[i -1].y_f = 287 + ( i - 1 ) * 60;
		menu->list[ i - 1 ].x_i = 198;
		menu->list[ i - 1 ].x_f = 202 + strlen( menu->list[i-1].name  )* 16;
	}
}

void printMenu( Menu * menu ,  char *video_base, char *table)
{
	drawSpriteTitle( 250 , 50 , video_base);
	int i = 0;
	for ( i = 0 ; i < menu->num_options ; ++i )
	{
		if ( menu->list[i].select == 0 )
		{
			drawBox( 198 , 240  + i* 60 ,  202 + strlen( menu->list[i].name  )* 16  , 287  + i* 60 , BACKCOLOR , 1 , video_base );
			drawStringAt(menu->list[i].name, 200, 250 + i* 60 , FORECOLOR, BACKCOLOR, 2, video_base, table);
			drawBox( 198 , 240  + i* 60 ,  202 + strlen( menu->list[i].name  )* 16  , 287  + i* 60 , FORECOLOR , 0 , video_base );
		}
		else
		{
			drawBox( 198 , 240  + i* 60 ,  202 + strlen( menu->list[i].name  )* 16  , 287  + i* 60 , FORECOLOR , 1 , video_base );
			drawStringAt(menu->list[i].name, 200, 250 + i * 60 , BACKCOLOR, FORECOLOR, 2, video_base, table);
		}
		
	}
}

Bool updateMenu( int sc , Menu * menu )
{
	int i = 0;
	if ( menu->list[0].select == 1 && sc == SC_UP )
	{
		menu->list[0].select = 0;
		menu->list[menu->num_options-1].select = 1;
		return true;
	}
	if ( menu->list[menu->num_options-1].select == 1 && sc == SC_DOWN )
	{
		menu->list[0].select = 1;
		menu->list[menu->num_options-1].select = 0;
		return true;
	}
	for ( i = 0 ; i < menu->num_options ; ++i )
	{
		if ( menu->list[i].select == 1 && sc == SC_UP )
		{
			menu->list[i].select = 0;
			menu->list[i-1].select = 1;
			return true;
		}
		if ( menu->list[i].select == 1 && sc == SC_DOWN )
		{
			menu->list[i].select = 0;
			menu->list[i+1].select = 1;
			return true;
		}
	}
	return false;
}

int selectedOption( Menu * menu)
{
	int option = -1 ;
  int i = 0;
	for ( i = 0 ; i < menu->num_options ; ++i )
	{
		if ( menu->list[i].select == 1 )
		{
			option = i;
			break;
		}
	}
	return option;
}

extern uchar mouse[3];
extern int num_byte;
extern int pack_completo;

void sub_menu( Menu * menu , char *video_base, char *table,  Word base , Player ** bestPlayers  , int mouse_sup )
{
#ifndef EMBED
  void kbd_isr(void);
  _go32_dpmi_seginfo old_kbd_irq;
  install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
   int back[4];

#else
  int scancode = 0;
  mouse_sup = 0;
#endif
  clear_screen(BACKCOLOR, video_base);
  printMenu(menu , video_base, table);
  int last_sc = 0 , option = 0;
  scancode = 0;
  do
	{
#ifdef EMBED
	  if (hasKey())
	    scancode = GetScanCode();
#endif
        option = 0;
#ifndef EMBED
		if ( mouse_sup != 0 )
		{
			if ( pack_completo )
			{
				parse_mouse_event(mouse);
				pack_completo = 0;
			}
			drawMouse( back , video_base );
			if ( ( option = evaluateMouse( menu ) ) != 0 )
				printMenu( menu , video_base, table);				
		}
#endif
		if(scancode != last_sc  || option == 2 )
		{
			last_sc = scancode;
      			if (scancode == SC_RET  || option == 2 )
			{
				option = selectedOption(menu);
				switch(option)
				{
					case 0 :
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						clear_screen(BACKCOLOR, video_base);
						play_single( video_base, table , bestPlayers);
						scancode = 0;
						last_sc = 0;
						printMenu(menu , video_base, table);
					#ifndef EMBED
						install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
					#endif
					    break;
          
					case 1 :
					
					clear_screen(BACKCOLOR, video_base);
					if ( sync_server( base , video_base, table) == false )
					{
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						drawStringAt("                       ", 300, 250, FORECOLOR, BACKCOLOR, 2, video_base, table);
						drawStringAt("Connection failed", 300, 250, FORECOLOR, BACKCOLOR, 2, video_base, table);
						getKey();
					#ifndef EMBED
						install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
					#endif
						clear_screen(BACKCOLOR, video_base);
				     	printMenu(menu , video_base, table);
						break;
					}
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						clear_screen(BACKCOLOR, video_base);
						play_server( base , video_base , table , bestPlayers);
						printMenu(menu , video_base, table);
					#ifndef EMBED
						install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
					#endif
						break;

					case 2 : 
					clear_screen(BACKCOLOR, video_base);
					if ( sync_client( base , video_base, table) == false )
					{
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						drawStringAt("                        ", 300, 250, FORECOLOR, BACKCOLOR, 2, video_base, table);
						drawStringAt("Connection failed...", 300, 250, FORECOLOR, BACKCOLOR, 2, video_base, table);
						getKey();
					#ifndef EMBED
						install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
					#endif
						clear_screen(BACKCOLOR, video_base);
						printMenu(menu , video_base, table);
						break;
					}
					#ifndef EMBED
						reinstall_asm_irq_handler(KBD_IRQ , &old_kbd_irq);
					#endif
						play_client( base , video_base , table , bestPlayers);
						printMenu(menu , video_base, table);
					#ifndef EMBED
						install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
					#endif
						break;

					case 3 : menu->list[3].select = 0;
					         menu->list[0].select = 1;
					         return;
					         break;
				}
				option = 0;
			}
		  if ( updateMenu( last_sc , menu) )
			  printMenu(menu, video_base, table);
		}
	} while (last_sc != 1);
}


